<?php

if ($_REQUEST['showStartTime']) {
   $long_time = ($_REQUEST['date'] . ' ' . $_REQUEST['showStartTime'] . ':00');
//   if ($_POST['showLength']) $short_time = ($_POST['date'] . ' ' . $_POST['showLength'] . ':00');
   if ($_REQUEST[showLength]) {
      switch ($_REQUEST[showLengthUnit]) {
         default:
            $showLength = $_REQUEST[showLength] * 3600;  // update time to seconds
            break;
         case 'min':
            $showLength = $_REQUEST[showLength] * 60;
            break;
         case 'hour':
            $showLength = $_REQUEST[showLength] * 3600;
            break;
         case 'day':
            $showLength = $_REQUEST[showLength] * 86400;
            break;
         case 'week':
            $showLength = $_REQUEST[showLength] * 604800;
            break;
         case 'month':
            $showLength = $_REQUEST[showLength] * 2592000;
            break;
         case 'year':
            $showLength = $_REQUEST[showLength] * 31536000;
            break;
      }
      $short_time = date('Y-m-d H:i:s', ( strtotime($long_time) + $showLength ) );
   }
//   if ($_REQUEST['showLength']) $short_time = date('Y-m-d H:i:s', ( (strtotime($long_time)) + ($_REQUEST['showLength']*3600) ));
}

$three_hours_ago  = date('Y-m-d H:i:s' ,mktime( date('H') - 3, date('i'), date('s'), date("m"),  date("d"),  date("Y")));
//mysql_select_db($DB_NAME, $view_connect);
$ten_minutes_ago  = date('Y-m-d H:i:s' ,mktime( date('H'), date('i') - 10, date('s'), date("m"),  date("d"),  date("Y")));
//mysql_select_db($DB_NAME, $view_connect);
if (!$long_time) $long_time = $three_hours_ago;
if (!$short_time) $short_time = $nowstamp;
$airplaysql = "SELECT `timestamp`,`trackno`,`track`,`artist`,`album`,`request`,`status`,`slot`,`user` FROM `airplay` WHERE (((`timestamp`>'".mysql_real_escape_string($long_time)."' AND `timestamp`<='".mysql_real_escape_string($short_time)."'))) ORDER BY `timestamp` DESC";
$pre_airplay_result = $airplayresult = mysql_query($airplaysql) or die(mysql_error());
?>

<form name="AirplayAdd" action="<?=$_SERVER[PHP_SELF]?>" method="post">
<fieldset>
<legend>Display Options</legend>
<?=$three_hours_ago?>
Date: <input size="10" type="text" name="date" value="<?php 
if ($_REQUEST['date']) echo ($_REQUEST['date']);
else echo(date('Y-m-d'));
?>" tabindex="9" id="date" size="15" /> <a href="#"><img src="./themes/<?=$theme?>/img/calendar-16.gif"  id="date_selector" /></a><br />
Start time: <input size="6" type="text" name="showStartTime" value="<?php 
if ($_REQUEST['showStartTime']) echo($_REQUEST[showStartTime]);
else echo(date('H:i', strtotime($three_hours_ago)));
?>" />
Length: <input type="text" size="3" name="showLength" value="<?php 
if ($_REQUEST[showLength]) echo ($_REQUEST[showLength]);
else echo('3');
?>" /><select name="showLengthUnit">
<option value="min" <?php 
if ($_REQUEST[showLengthUnit] === 'min') {
   echo(' selected="selected"');
   $setShowLengthUnit = true;
}
?>>minutes</option>
<option value="hour" <?php 
if ( ($_REQUEST[showLengthUnit] === 'hour') || (!$_REQUEST[showLengthUnit])) {
   echo(' selected="selected"');
   $setShowLengthUnit = true;
}
?>>hours</option>
<option value="day" <?php 
if ($_REQUEST[showLengthUnit] === 'day') {
   echo(' selected="selected"');
   $setShowLengthUnit = true;
}
?>>days</option>
<option value="week" <?php 
if ($_REQUEST[showLengthUnit] === 'week') {
   echo(' selected="selected"');
   $setShowLengthUnit = true;
}
?>>weeks</option>
<option value="month" <?php 
if ($_REQUEST[showLengthUnit] === 'month') {
   echo(' selected="selected"');
   $setShowLengthUnit = true;
}
?>>months</option>
<option value="year" <?php 
if ($_REQUEST[showLengthUnit] === 'year') {
   echo(' selected="selected"');
   $setShowLengthUnit = true;
}
?>>years</option>
</select><br />
<input type="hidden" name="loading" value="true" />
<input type="hidden" name="mode" value="showaudit" />
<input type="hidden" name="cat" value="<?=$cat?>" />
<input type="submit" value="Go Go!" />
</fieldset>
</form>

<table style="border: 2px solid black; background-color: #d6e6ff">
<tr align="center" style="background-color: #7e96ba; color:white">
<td>slot</td><td>track</td><td>track title</td><td>artist</td><td>album</td><td>new</td><td>old</td><td>req</td><td>time</td></tr>
</tr>
<?php
$lasttimestamp = $short_time; //$nowstamp; 
$newcounter = 0;
$oldcounter = 0;
$requestcounter = 0;
$q =0; //we use this to count for element IDs to help out javascript stuff
while (($air = $airplayrow = mysql_fetch_array($airplayresult))) {
   if ( (!$airOldDay) || ($airOldDay !== date('N', strtotime($air[timestamp]))) ) {
      $airOldDay = date('N', strtotime($air[timestamp]));
      ?><tr><td colspan="9"><h3><?php echo ( date('l, F jS Y', strtotime($air[timestamp])) ) ?></h3></td></tr><?php 
   }
   if ( (!$airOldUsr) || ($airOldUsr !== $air[user]) ) {
      $usrNameQuery = mysql_query("SELECT `name` FROM `users` WHERE (`id`='".$air[user]."') LIMIT 1 ");
      $airUsrName = mysql_result($usrNameQuery, 0);
      $airOldUsr = $air[user];
      ?><tr><td colspan="9"><h3>User: <?=$airUsrName?></h3></td></tr><?php 
   }
	//start getting info for traffic log
	$traffic_sql = "
	   SELECT * FROM `traffic` 
	   WHERE 
	   (
	      (`timestamp`<='".mysql_real_escape_string($lasttimestamp)."')  
	      AND 
	      (`timestamp`>='".mysql_real_escape_string($airplayrow[0])."')
         AND
         (`dummy`!='1')
      ) 
      ORDER BY `timestamp` DESC";
	$traffic_result = mysql_query($traffic_sql) or die(mysql_error());
	while($traffic_row = mysql_fetch_array($traffic_result)) {
	
		//this will give us the proper event name from the DB:
		$eventname = mysql_result(mysql_query("SELECT `name` FROM `event` WHERE `id`='".mysql_real_escape_string($traffic_row[eventtype])."' LIMIT 1"), 0);
		
		?>
		<tr style="background-color: #c0c0c0"><td colspan="2" style="border-bottom: 1px dotted black; vertical-align:middle; text-align:center"><?=date("h:i", strtotime($traffic_row[timestamp]))?></td><td><?=$eventname?></td><td colspan="5"><?=$traffic_row[comment]?></td><td></td></tr>
		<?php
	}
	$status = $airplayrow[6];
	if ($status == 1 || $status == 2) {
		$new = '<img height="16" width="16" alt="new checkmark" src="./themes/'.$theme.'/img/check.png" title="NEW!" />';
		$newcounter++;
		$old = '<img height="16" width="16" src="./themes/'.$theme.'/img/blank.png" alt=" " />';
	} else {
	  $old = '<img height="16" width="16" alt="old checkmark" src="./themes/'.$theme.'/img/check.png" title="old." />';
	  $new = '<img height="16" width="16" src="./themes/'.$theme.'/img/blank.png" alt=" " />';
	  $oldcounter++;
	}
	if ($airplayrow[5]) {
		$request = '<img height="16" width="16" alt="request checkmark" src="./themes/' . $theme . '/img/check.png" title="a request!" />';
		$requestcounter++;
	} else {
	$request = '<img height="16" width="16" src="./themes/' . $theme . '/img/blank.png" alt=" " />';
	}
?>
<tr class="highlighted" style="font-size: small;">
<td style="border-right: 1px solid black; border-bottom: 1px dotted black"><?=$airplayrow[7]?></td>
<td style="border-right: 1px solid black; border-bottom: 1px dotted black"><?=$airplayrow[1]?></td>
<td style="border-right: 1px solid black; border-bottom: 1px dotted black" title="<? print("'$airplayrow[2]' by '$airplayrow[3]'")?>">

<span id="air_trunc<?=$q?>" class="air_normal" ondblclick="if (this.className=='air_normal') {document.getElementById('air_trunc<?=$q?>').className='hidden'; document.getElementById('air_untrunc<?=$q?>').className='air_bigger'}"><?=trunc($airplayrow[2],23)?></span>
<span id="air_untrunc<?=$q?>" class="hidden" ondblclick="if (this.className=='air_bigger') {this.className='hidden'; document.getElementById('air_trunc<?=$q?>').className='air_normal'}"><?=$airplayrow[2]?></span>

</td>
<td style="border-right: 1px solid black; border-bottom: 1px dotted black" title="<? print("'$airplayrow[2]' by '$airplayrow[3]'")?>"><?=trunc($airplayrow[3],23)?></td>
<td style="border-right: 1px solid black; border-bottom: 1px dotted black" title="<?="album: " . $airplayrow[4]?>"><?=trunc($airplayrow[4],25)?></td>
<td style="border-right: 1px solid black; border-bottom: 1px dotted black; text-align:center; vertical-align:middle;"><?=$new?></td>
<td style="border-right: 1px solid black; border-bottom: 1px dotted black; text-align:center; vertical-align:middle"><?=$old?></td>
<td style="border-right: 1px solid black; border-bottom: 1px dotted black; text-align:center; vertical-align:middle"><?=$request?></td>
<td><?php echo( date('H:i:s', strtotime($air[timestamp]) ) ) ?></td>
</tr><?php
	unset($request);
	$lasttimestamp = $airplayrow[0];
	$q++;
}
?>
<?php
if (!$lasttimestamp) $lasttimestamp = $long_time;
$traffic_sql = "SELECT * FROM `traffic` WHERE ((`timestamp`<'".mysql_real_escape_string($lasttimestamp)."')  AND (`timestamp`>='".mysql_real_escape_string($long_time)."')) ORDER BY `timestamp` DESC";
$traffic_result = mysql_query($traffic_sql) or die(mysql_error());
while (($traffic_row = mysql_fetch_assoc($traffic_result)) && !(mysql_fetch_row($airplayresult))) {
	//start getting info for traffic log
	//this will give us the proper event name from the DB:
	$eventname = mysql_result(mysql_query("SELECT `name` FROM `event` WHERE `id`='".mysql_real_escape_string($traffic_row[eventtype])."' LIMIT 1"), 0);
	?>
	<tr style="background-color: #c0c0c0"><td colspan="2" style="border-bottom: 1px dotted black; vertical-align:middle; text-align:center;"><?=date("h:i", strtotime($traffic_row[timestamp]))?></td><td><?=$eventname?></td><td colspan="5"><?=$traffic_row[comment]?></td><td></td></tr>
	<?php
}
?>
<tr><td colspan="5"> totals:</td><td><?=$newcounter?></td><td><?=$oldcounter?></td><td><?=$requestcounter?></td>
</tr></table>

<?php 
$logoutSql = "SELECT * FROM `logout` WHERE (( (`logout`>'".mysql_real_escape_string($long_time)."') AND (`logout`<='".mysql_real_escape_string($short_time)."') )) ORDER BY `logout` ASC";
$logoutQuery = mysql_query($logoutSql);
while ($logout = mysql_fetch_array($logoutQuery)) {
   if ($logout[comment]) {
      $usr = mysql_fetch_array(mysql_query("SELECT FROM `users` WHERE (`id`='".$logout[user]."') LIMIT 1 "));
      echo($usr[username].' ('.$logout[logout].') :'.$logout[comment].'<br />');
   }
}
?>

<script type="text/javascript">
<!-- //this is for the javascript calendar -- a beautiful GPL mini date selector courtesy of the folks at dynarch.com
Calendar.setup(
{
inputField : "date", // ID of the input field
ifFormat : "%Y-%m-%d", // the date format
button : "date_selector" // ID of the button
}
);
//end date selector stuff -->
</script>

<!--</td></tr></table> -->